home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nejlepší hry
/
Nejlepsi hry.iso
/
hry
/
plane arcade
/
planearcade.exe
/
tank3.bmp
/
types.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-08-21
|
20KB
|
865 lines
#include "main.h"
//-----------------------------------------------------------------------------
// Name: GetColor()
// Desc: pretazena funkcia
//-----------------------------------------------------------------------------
COLOR GetColor(float R,float G,float B)
{
return GetColor(1.0f,R,G,B);
}
//-----------------------------------------------------------------------------
// Name: GetColor()
// Desc: funckia vrati COLOR
//-----------------------------------------------------------------------------
COLOR GetColor(float A,float R,float G,float B)
{
COLOR Vys;
Vys.R = R;
Vys.B = B;
Vys.G = G;
Vys.A = A;
return Vys;
}
//-----------------------------------------------------------------------------
// Name: GetColor()
// Desc: vytvori D3D maticu
//-----------------------------------------------------------------------------
D3DXMATRIXA16 GetMatrix(VECTOR3D Pos,VECTOR3D Rot,VECTOR3D Sca)
{
D3DXMATRIXA16 matAll;
D3DXMATRIXA16 matRot;
D3DXMATRIXA16 matPos;
D3DXMATRIXA16 matSca;
//rotation//
D3DXMatrixRotationYawPitchRoll(&matRot,Rot.Y,Rot.X,Rot.Z);
//position//
D3DXMatrixTranslation( &matPos, Pos.X,Pos.Y,Pos.Z);
//scaling//
D3DXMatrixScaling( &matSca, Sca.X,Sca.Y,Sca.Z);
D3DXMatrixIdentity(&matAll);
D3DXMatrixMultiply(&matAll,&matAll,&matSca);
D3DXMatrixMultiply(&matAll,&matAll,&matRot);
D3DXMatrixMultiply(&matAll,&matAll,&matPos);
return matAll;
}
//-----------------------------------------------------------------------------
// Name: Sub()
// Desc: Rozdiel vektorov
//-----------------------------------------------------------------------------
void Sub(VECTOR3D * V,VECTOR3D A,VECTOR3D B)
{
V->X = A.X - B.X;
V->Y = A.Y - B.Y;
V->Z = A.Z - B.Z;
}
//-----------------------------------------------------------------------------
// Name: Add()
// Desc: spocita vektory
//-----------------------------------------------------------------------------
void Add(VECTOR3D * V,VECTOR3D A,VECTOR3D B)
{
V->X = A.X + B.X;
V->Y = A.Y + B.Y;
V->Z = A.Z + B.Z;
}
//-----------------------------------------------------------------------------
// Name: Mul()
// Desc: Vynasobi vektory
//-----------------------------------------------------------------------------
void Mul(VECTOR3D * V,VECTOR3D A,VECTOR3D B)
{
V->X = A.X * B.X;
V->Y = A.Y * B.Y;
V->Z = A.Z * B.Z;
}
//-----------------------------------------------------------------------------
// Name: Cross()
// Desc: Vypocita normalu dvoch vektorov A,B
//-----------------------------------------------------------------------------
void Cross(VECTOR3D * V,VECTOR3D A,VECTOR3D B)
{
V->X = B.Y * A.Z - A.Y * B.Z;
V->Y = B.Z * A.X - A.Z * B.X;
V->Z = B.X * A.Y - A.X * B.Y;
}
//-----------------------------------------------------------------------------
// Name: Dot()
// Desc: skalarny sucin
//-----------------------------------------------------------------------------
float Dot(VECTOR3D A,VECTOR3D B)
{
return (B.X * A.X) + (B.Y * A.Y) + (B.Z * A.Z);
}
//-----------------------------------------------------------------------------
// Name: Absolute()
// Desc: Vypocita absolutnu hodnotu vektora
//-----------------------------------------------------------------------------
float Absolute(VECTOR3D V)
{
return (float)
( sqrt(
(V.X * V.X) +
(V.Y * V.Y) +
(V.Z * V.Z)
) );
}
//-----------------------------------------------------------------------------
// Name: Normalize()
// Desc: Upravy vektor tak aby jeho Absolutna hodnota = 1
//-----------------------------------------------------------------------------
void Normalize(VECTOR3D * V)
{
float Abs = Absolute(*V);
V->X = V->X / Abs;
V->Y = V->Y / Abs;
V->Z = V->Z / Abs;
}
//-----------------------------------------------------------------------------
// Name: Get3D()
// Desc: Vrati VECTOR3D
//---------------------------------------------------------------------------
VECTOR3D Get3D(float X,float Y,float Z)
{
VECTOR3D V;
V.X = X;
V.Y = Y;
V.Z = Z;
return V;
}
//-----------------------------------------------------------------------------
// Name: Get2D()
// Desc: Vrati VECTOR2D
//---------------------------------------------------------------------------
VECTOR2D Get2D(float X,float Y)
{
VECTOR2D V;
V.X = X;
V.Y = Y;
return V;
}
//-----------------------------------------------------------------------------
// Name: Project()
// Desc: Premietne 3D bod na 2D obrazovku
//---------------------------------------------------------------------------
VECTOR3D Project(VECTOR3D B)
{
VECTOR3D V;
D3DXVECTOR3 In;
D3DXVECTOR3 Out;
D3DXMATRIX world;
//vytvori prazdnu maticu
world = GetMatrix(Get3D(0.0f,0.0f,0.0f),Get3D(0.0f,0.0f,0.0f),Get3D(1.0f,1.0f,1.0f));
In.x = B.X;
In.y = B.Y;
In.z = B.Z;
D3DXVec3Project(&Out,&In,&Engine.Viewport,&matProj,&matView,&world);
if (Out.x < 0) Out.x = -1000;
if (Out.y < 0) Out.y = -1000;
if (Out.x > Engine.Width) Out.x = -1000;
if (Out.y > Engine.Height) Out.y = -1000;
if (Out.z < Engine.Max)
{
V.X = Out.x ;
V.Y = Out.y ;
V.Z = 0.0f ;
}
else
{
V.X = -1000 ;
V.Y = -1000;
V.Z = 0.0f;
}
return V;
}
//-----------------------------------------------------------------------------
// Name: Project()
// Desc: Premietne 2D bod do 3D priestoru
//---------------------------------------------------------------------------
VECTOR3D UnProject(VECTOR3D B)
{
VECTOR3D V;
D3DXVECTOR3 In;
D3DXVECTOR3 Out;
D3DXMATRIX world;
world = GetMatrix(Get3D(0.0f,0.0f,0.0f),Get3D(0.0f,0.0f,0.0f),Get3D(1.0f,1.0f,1.0f));
In.x = B.X;
In.y = B.Y;
In.z = 0.0f;
D3DXVec3Unproject(&Out,&In,&Engine.Viewport,&matProj,&matView,&world);
V.X = Out.x ;
V.Y = Out.y ;
V.Z = Out.z ;
return V;
}
//------------------------------------------------------------------
// Name: GetPlane
// Desc: vrati PLANE
//------------------------------------------------------------------
PLANE GetPlane(VECTOR3D Normal,float D)
{
PLANE P;
P.Normal = Normal;
P.D = D;
return P;
}
//------------------------------------------------------------------
// Name: CreatePlaneFace
// Desc: Vytvori rovinu z trojholnika udaneho bodmi B1,B2,B3
//------------------------------------------------------------------
PLANE CreatePlaneFace(VECTOR3D B1,VECTOR3D B2,VECTOR3D B3)
{
VECTOR3D V1,V2;
PLANE P;
Sub(&V1,B1,B2);
Sub(&V2,B3,B2);
Cross(&P.Normal,V2,V1);
Normalize(&P.Normal);
P.D = -(P.Normal.X*B2.X)
-(P.Normal.Y*B2.Y)
-(P.Normal.Z*B2.Z);
return P;
}
//------------------------------------------------------------------
// Name: CreatePlanePoint
// Desc: Vytvori rovinu z dvoch bodov B1 lezi na rovine
// rozdiel B2 a B1 vytvara normalu
//------------------------------------------------------------------
PLANE CreatePlanePoint(VECTOR3D B1,VECTOR3D B2)
{
PLANE P;
Sub(&P.Normal,B2,B1);
Normalize(&P.Normal);
P.D = -(P.Normal.X*B1.X)
-(P.Normal.Y*B1.Y)
-(P.Normal.Z*B1.Z);
return P;
}
//------------------------------------------------------------------
// Name: PointPlane
// Desc: Zisti na ktorej strane roviny lezi bod, ak na zadnej vysledok
// je mensi ako nula ak na prednej vysledok je vacsi ako nula
//------------------------------------------------------------------
float PointPlane(PLANE P,VECTOR3D B)
{
float Vys;
Vys = (P.Normal.X*B.X)
+(P.Normal.Y*B.Y)
+(P.Normal.Z*B.Z)
+P.D;
return Vys;
}
//------------------------------------------------------------------
// Name: CalcPriesEdge
// Desc: Vypocita priesecnik roviny P s priamkov udanou B1 a B2, priesecnik,
// sa ulozi do B, funckia vrati true ak ma priamka priesecnik s rovinou P
//------------------------------------------------------------------
bool CalcPriesEdge(VECTOR3D *B,PLANE P,VECTOR3D B1,VECTOR3D B2)
{
float Vys1 = PointPlane(P,B1);
float Vys2 = PointPlane(P,B2);
if ((Vys1<0.0f) && (Vys2>0.0f) ||
(Vys1>0.0f) && (Vys2<0.0f))
{
//smerovy vektor priamky
VECTOR3D Sme;
Sub(&Sme,B2,B1);
Normalize(&Sme);
//vypocet parametru T pomocou ktoreho sa zisti priesecnik
float T = (-P.D - (P.Normal.X*B1.X) - (P.Normal.Y*B1.Y) - (P.Normal.Z*B1.Z))/
((P.Normal.X*Sme.X) + (P.Normal.Y*Sme.Y) + (P.Normal.Z*Sme.Z));
//priesecnik
B->X = B1.X + (Sme.X * T);
B->Y = B1.Y + (Sme.Y * T);
B->Z = B1.Z + (Sme.Z * T);
return true;
}
else
{
return false;
}
}
//------------------------------------------------------------------
// Name: CalcPries
// Desc: Vypocita priesecnik troch rovin, priesecnik sa ulozi do B,
// v pripade ze roviny nemaju jeden priesecnik funkcia vrati false
//------------------------------------------------------------------
bool CalcPriesPlane(VECTOR3D *B,PLANE P1,PLANE P2,PLANE P3)
{
//zisti ci su roviny rovnobe₧ne
float denom;
VECTOR3D Pom1;
Cross(&Pom1,P2.Normal,P3.Normal);
denom = Dot(Pom1,P1.Normal);
if (denom == 0.0f)
{
return false;
}
//zistenie priesecnika
VECTOR3D C1,C2,C3;
VECTOR3D A1,A2,A3;
Cross(&C1,P2.Normal,P3.Normal);
Cross(&C2,P3.Normal,P1.Normal);
Cross(&C3,P1.Normal,P2.Normal);
A1.X = C1.X * P1.D;
A1.Y = C1.Y * P1.D;
A1.Z = C1.Z * P1.D;
A2.X = C2.X * P2.D;
A2.Y = C2.Y * P2.D;
A2.Z = C2.Z * P2.D;
A3.X = C3.X * P3.D;
A3.Y = C3.Y * P3.D;
A3.Z = C3.Z * P3.D;
B->X = -A1.X - A2.X - A3.X;
B->Y = -A1.Y - A2.Y - A3.Y;
B->Z = -A1.Z - A2.Z - A3.Z;
B->X = B->X / denom;
B->Y = B->Y / denom;
B->Z = B->Z / denom;
return true;
}
//------------------------------------------------------------------
// Name: CalcAngleCentre()
// Desc: Vypocitaj uhol zadany bodmi, pricom vrchol je S
//------------------------------------------------------------------
float CalcAngleCentre(VECTOR3D S,VECTOR3D P1, VECTOR3D P2)
{
//pomocne vektory
VECTOR3D A;
VECTOR3D B;
float medzi; //medzivypocet
float vysledok; //konecky vysledok
//z troch bodov vypoΦφtame de vektory
//z ktor²ch vypoΦφtame uhol medzi
Sub(&A,P1,S);
Sub(&B,P2,S);
//vypocet absolutnych hodnot vektorov
float AbsA = Absolute(A);
float AbsB = Absolute(B);
//skalarny sucin / absolutne hodnoty vektorov
medzi = (Dot(A,B)) / (AbsA * AbsB);
//a nakoniec vypocet uhla
vysledok = acosf(medzi);
//nezaporny
if (vysledok < 0.0f) vysledok = -vysledok;
return vysledok;
}
//------------------------------------------------------------------
// Name: CalcAngleVector()
// Desc: vypocitaj uhol ktore zvieraju dve vektory
//------------------------------------------------------------------
float CalcAngleVector(VECTOR3D P1, VECTOR3D P2)
{
float medzi; //medzivypocet
float vysledok; //konecky vysledok
//vypocet absolutnych hodnot vektorov
float AbsP1 = Absolute(P1);
float AbsP2 = Absolute(P2);
//skalarny sucin / absolutne hodnoty vektorov
medzi = (Dot(P1,P2)) / (AbsP1 * AbsP2 );
//a nakoniec vypocet uhla
vysledok = acosf(medzi);
return vysledok;
}
//------------------------------------------------------------------
// Name: CalcDistance()
// Desc: Vypocet vzdialenosti
//------------------------------------------------------------------
float CalcDistance(VECTOR3D B1,VECTOR3D B2)
{
VECTOR3D Vec;
Sub(&Vec,B1,B2);
float Abs = Absolute(Vec);
return Abs ;
}
//------------------------------------------------------------------
// Name: TransformPoint()
// Desc: transformuje vector3D
//------------------------------------------------------------------
VECTOR3D TransformPoint(VECTOR3D Point,D3DXMATRIXA16 Matica)
{
VECTOR3D Vys;
D3DXVECTOR3 PIn;
D3DXVECTOR3 POut;
PIn.x = Point.X;
PIn.y = Point.Y;
PIn.z = Point.Z;
D3DXVec3TransformCoord( &POut,&PIn,&Matica);
Vys.X = POut.x;
Vys.Y = POut.y;
Vys.Z = POut.z;
return Vys;
}
//------------------------------------------------------------------
// Name: TransformPlane()
// Desc: transformuje rovinu
//------------------------------------------------------------------
PLANE TransformPlane(PLANE Plane,D3DXMATRIXA16 Matica)
{
PLANE Vys;
D3DXPLANE PIn;
D3DXPLANE POut;
PIn.a = Plane.Normal.X;
PIn.b = Plane.Normal.Y;
PIn.c = Plane.Normal.Z;
PIn.d = Plane.D;
D3DXPlaneTransform(&POut,&PIn,&Matica);
Vys.Normal.X = POut.a;
Vys.Normal.Y = POut.b;
Vys.Normal.Z = POut.c;
Vys.D = POut.d;
return Vys;
}
//------------------------------------------------------------------
// Name: TransformNormal()
// Desc: transformuje normalovy vektor
//------------------------------------------------------------------
VECTOR3D TransformNormal(VECTOR3D Point,D3DXMATRIXA16 Matica)
{
VECTOR3D Vys;
D3DXVECTOR3 PIn;
D3DXVECTOR3 POut;
PIn.x = Point.X;
PIn.y = Point.Y;
PIn.z = Point.Z;
D3DXVec3TransformNormal( &POut,&PIn,&Matica);
Vys.X = POut.x;
Vys.Y = POut.y;
Vys.Z = POut.z;
return Vys;
}
//------------------------------------------------------------------
// Name: UnTransformPoint()
// Desc: untransformuje vector3D
//------------------------------------------------------------------
VECTOR3D UnTransformPoint(VECTOR3D Point,D3DXMATRIXA16 Matica)
{
VECTOR3D Vys;
D3DXVECTOR3 PIn;
D3DXVECTOR4 POut;
PIn.x = Point.X;
PIn.y = Point.Y;
PIn.z = Point.Z;
D3DXMatrixInverse(&Matica,NULL,&Matica);
D3DXVec3Transform( &POut,&PIn,&Matica);
Vys.X = POut.x;
Vys.Y = POut.y;
Vys.Z = POut.z;
return Vys;
}
//------------------------------------------------------------------
// Name: GetMaterial()
// Desc: Vrati material
//------------------------------------------------------------------
D3DMATERIAL9 GetMaterial(COLOR Ambient,
COLOR Diffuse,
COLOR Specular,
COLOR Emissive,float Power)
{
D3DMATERIAL9 Vys;
Vys.Ambient.a = Ambient.A;
Vys.Ambient.r = Ambient.R;
Vys.Ambient.g = Ambient.G;
Vys.Ambient.b = Ambient.B;
Vys.Emissive.a = Emissive.A;
Vys.Emissive.r = Emissive.R;
Vys.Emissive.g = Emissive.G;
Vys.Emissive.b = Emissive.B;
Vys.Diffuse.a = Diffuse.A;
Vys.Diffuse.r = Diffuse.R;
Vys.Diffuse.g = Diffuse.G;
Vys.Diffuse.b = Diffuse.B;
Vys.Specular.a = Specular.A;
Vys.Specular.r = Specular.R;
Vys.Specular.g = Specular.G;
Vys.Specular.b = Specular.B;
Vys.Power = Power;
return Vys;
}
//------------------------------------------------------------------
// Name: CollisionBoxEdge
// Desc: Kolizia medzi kvadrom a useckou
//------------------------------------------------------------------
bool CollisionBoxEdge(VECTOR3D P1,VECTOR3D P2,VECTOR3D Min,VECTOR3D Max)
{
if(((P1.X < Max.X) && (P2.X > Min.X)) ||
((P1.X > Min.X) && (P2.X < Max.X)))
{
if(((P1.Y < Max.Y) && (P2.Y > Min.Y)) ||
((P1.Y > Min.Y) && (P2.Y < Max.Y)))
{
if(((P1.Z < Max.Z) && (P2.Z > Min.Z)) ||
((P1.Z > Min.Z) && (P2.Z < Max.Z)))
{
return true;
}
}
}
return false;
}
//-----------------------------------------------------------------------------
// Name: FixedToRelative
// Desc: z fixneho rozlisenia sa prepocitavaju suradnice do relativneho
//-----------------------------------------------------------------------------
float FTRX(float Value)
{
return (Value/800.0f)*Engine.Width;
}
float FTRY(float Value)
{
return (Value/600.0f)*Engine.Height;
}
//-----------------------------------------------------------------------------
// Name: getRandomMinMax()
// Desc: Gets a random number between min/max boundaries
//-----------------------------------------------------------------------------
float RandomMinMax( float fMin, float fMax )
{
float fRandNum = (float)rand () / RAND_MAX;
return fMin + (fMax - fMin) * fRandNum;
}
//-----------------------------------------------------------------------------
// Name: RandomVector()
// Desc: Generates a random vector where X,Y, and Z components are between
// -1.0 and 1.0
//-----------------------------------------------------------------------------
VECTOR3D RandomVector()
{
VECTOR3D vVector;
// Pick a random Z between -1.0f and 1.0f.
vVector.Z = RandomMinMax( -1.0f, 1.0f );
// Get radius of this circle
float radius = (float)sqrt(1 - vVector.Z * vVector.Z);
// Pick a random point on a circle.
float t = RandomMinMax( -D3DX_PI, D3DX_PI );
// Compute matching X and Y for our Z.
vVector.X = (float)cosf(t) * radius;
vVector.Y = (float)sinf(t) * radius;
return vVector;
}
//----------------------------------------------------------------
//Name: EnvironmentMapping
//Desc: vypocita texture suradnice pre enviro mapping
//----------------------------------------------------------------
VECTOR2D EnvironmentMapping(VECTOR3D Point,
VECTOR3D Normal,
float Offset,
D3DXMATRIXA16 ViewMatrix)
{
VECTOR2D Vys;
Normal = TransformNormal(Normal,ViewMatrix);
Point = TransformPoint (Point,ViewMatrix);
//tex.u = max_u/2 + normal.x*(max_u/2)
//tex.v = max_v/2 + normal.y*(max_v/2)
Vys.X = 0.5f + Normal.X*(0.5f)+(Point.X/300.0f);
Vys.Y = 0.5f + Normal.Y*(0.5f)+(Point.Z/300.0f);
return Vys;
}
//----------------------------------------------------------------
//Name: BumpMapping
//Desc: vypocita texture suradnice pre bump mapping
//----------------------------------------------------------------
VECTOR2D BumpMapping(VECTOR3D Normal, VECTOR2D TexCoor,float Offset, D3DXMATRIXA16 ViewMatrix)
{
VECTOR2D Vys;
VECTOR3D N;
N = TransformNormal(Normal,ViewMatrix);
Vys.X = TexCoor.X + N.X*(Offset);
Vys.Y = TexCoor.Y + N.Y*(Offset);
return Vys;
}
//------------------------------------------------------------------
// Name: GetRotation
// Desc: zo smeroveho nenormalizovaneho vektoru urobi rotaciu ypr
//------------------------------------------------------------------
VECTOR3D GetRotationLok(VECTOR3D Pos, VECTOR3D Lok)
{
VECTOR3D Rot;
VECTOR3D Sme;
Sub(&Sme,Lok,Pos);
Sme.Y = 0.0f;
Normalize(&Sme);
Rot.Y = asinf(Sme.X);
if (Sme.Z <= 0.0f)
{
Rot.Y = 3.14f - Rot.Y;
}
Sub(&Sme,Lok,Pos);
Normalize(&Sme);
Rot.X = -asinf(Sme.Y);
return Rot ;
}
//------------------------------------------------------------------
// Name: GetRotationSme
// Desc: zo smeroveho nenormalizovaneho vektoru urobi rotaciu ypr
//------------------------------------------------------------------
VECTOR3D GetRotationSme(VECTOR3D Sme)
{
VECTOR3D Rot;
VECTOR3D S;
S = Sme;
S.Y = 0.0f;
Normalize(&S);
Rot.Y = asinf(S.X);
if (S.Z <= 0.0f)
{
Rot.Y = 3.14f - Rot.Y;
}
S = Sme;
Normalize(&S);
Rot.X = -asinf(S.Y);
return Rot ;
}
//------------------------------------------------------------------
// Name: FPS
// Desc: Funcie pre FPS
//------------------------------------------------------------------
float FPS = 1000.0f;
int iFPS = 1000;
__int64 counter1;
__int64 counter2;
__int64 freq;
void TimeStart()
{
QueryPerformanceCounter((LARGE_INTEGER *)&counter1);
}
void TimeEnd()
{
QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
QueryPerformanceCounter((LARGE_INTEGER *)&counter2);
FPS = (float)freq / ((float)(counter2-counter1));
iFPS = (int)FPS;
}
float Power(float Value)
{
return (30.0f/FPS)*Value;
}
float PowerTime(float Value)
{
return (1000.0f/FPS)*Value;
}